<template>
  <el-card style="width: 50%;margin:0 auto">
    <el-form  label-width="80px" size="small" ref="addForm">
      <!--文件上传-->
      <el-upload
          class="avatar-uploader"
          action="http://localhost:8888/file/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
      >
        <img v-if="form.avatarUrl" :src="form.avatarUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>

      <el-form-item label="用户名">
        <el-input v-model="form.username" disabled autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="form.nickname" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.email" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="联系方式">
        <el-input v-model="form.phone" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item  label="地址">
        <el-input type="textarea" v-model="form.address" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
<!--        <el-button @click="dialogFormVisible = false">取 消</el-button>-->
        <el-button type="primary" @click="save">确 定</el-button>
      </el-form-item>


    </el-form>
  </el-card>
</template>

<script>
export default {
  name: "Person",
  data(){
    return{
      form:{},//表单
      dialogFormVisible:false,//表单显示
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  created() {
    //获取用户信息
    // this.$axios.get('/user/username/'+this.user.username).then(res=>{
    //   this.form=res.data.data
    // }),
    this.getUser().then(res => {
      // console.log(res)
      this.form = res.data
    })
  },
  methods:{
    async getUser() {
      return (await this.$axios.get("/user/username/" + this.user.username)).data
    },
    //保存
    save(){
      this.$axios.post('/user',this.form).then(res=>{
        if(res.data.code==200){
          this.$message.success("保存成功")

          //触发父级更新User的方法
          this.$emit("refreshUser")

          //更新浏览器存储的用户信息
          this.getUser().then(res=>{
            res.data.token=JSON.parse(localStorage.getItem("user")).token
            localStorage.setItem("user",JSON.stringify(res.data))
          })

          this.$router.push('/')
        }else{
          this.$message.error("保存失败")
        }
      })
    },
    handleAvatarSuccess(res) {
      this.form.avatarUrl = res
    }
  }
}
</script>

<style>
.avatar-uploader {
  text-align: center;
  padding-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
.avatar {
  width: 138px;
  height: 138px;
  display: block;
}
</style>